<script>
export default {
  data() {
    return {
      deviceId: "",
    };
  },
  methods: {
    reConnect() {
      uni.closeBluetoothAdapter({
        success: (res) => {
          console.log("关闭蓝牙适配器成功", res);
          setTimeout(() => {
            uni.openBluetoothAdapter({
              success: (res) => {
                console.log("打开蓝牙适配器成功", res);

                uni.createBLEConnection({
                  deviceId: this.deviceId, // 从store中获取设备ID
                  success: (res) => {
                    console.log("连接成功", res);
                  },
                  fail: (err) => {
                    console.log("连接失败", err);
                  },
                });
              },
              fail: (err) => {
                console.log("打开蓝牙适配器失败", err);
              },
            });
          }, 1000);
        },
        fail: (err) => {
          console.log("关闭蓝牙适配器失败", err);
        },
      });
    },
  },
  onLaunch: function () {
    this.deviceId = uni.getStorageSync("deviceId");
    console.log("App Launch");
    this.reConnect();
  },
  onShow: function () {
    console.log("App Show");
  },
  onHide: function () {
    console.log("App Hide");
  },
};
</script>

<style>
/*每个页面公共css */
@import url("./static/main.css");
</style>
